﻿@{
    ViewBag.Title = "回复消息";
}
@model DTcms.Model.weixin_request_rule
<form id="form1" class="layui-form" url="SubscribeSave?action=@ViewBag.action&id=@ViewBag.id">
    @Html.AntiForgeryToken()
    <!--导航栏-->
    <div class="pageheader">
        <div class="page-bar">
            <ul class="page-breadcrumb">
                <li><a href="javascript:history.back(-1);" class="back"><i class="fa fa-mail-reply-all"></i> 返回上一页</a></li>
                <li><a href="javascript:;"><i class="iconfont icon-home1"></i> 首页</a> </li>
                <li><a href="javascript:;">应用管理</a> </li>
                <li><a href="javascript:;">微信管理</a> </li>
                <li><a href="javascript:;">@ViewBag.Position</a> </li>
            </ul>
        </div>
    </div>
    <!--/导航栏-->
    <!--内容-->
    <div class="layui-fluid">
        <div class="layui-tab layui-tab-card">
            <ul class="layui-tab-title" id="floatHead">
                <li class="layui-this">@ViewBag.Position</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <div class="layui-form-item">
                        <label class="layui-form-label">公众账户</label>
                        <div class="layui-input-block">
                            <select class="select" lay-filter="account" name="ddlAccountId" url="/webapi/weixinJson" selectValue="@ViewBag.id" promtion="请选择公众账户" datatype="*" errormsg="请选择公众账户！" sucmsg=" "><option value="">请选择公众账户...</option></select>
                            <span class="Validform_checktip">*当前的公众账户，可以切换。</span>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">消息类型</label>
                        <div class="layui-input-block">
                            <input name="hideId" type="hidden" value="@(Model==null?0:Model.id)" />
                            <input name="hideRequestType" type="hidden" value="@ViewBag.RequestType" />
                            @Html.RadioButton("rblResponseType", 0, Model != null ? Model.response_type == 1 : true, new { title = "文本回复", @lay_filter = "responseType" })
                            @Html.RadioButton("rblResponseType", 1, Model != null ? Model.response_type == 2 : false, new { title = "图文回复", @lay_filter = "responseType" })
                            @Html.RadioButton("rblResponseType", 2, Model != null ? Model.response_type == 3 : false, new { title = "语音回复", @lay_filter = "responseType" })
                        </div>
                    </div>
                    <div class="text layui-form-item">
                        <label class="layui-form-label">回复内容</label>
                        <div class="layui-input-block">
                            @Html.TextArea("txtContent", Model != null && Model.response_type == 1 ? Model.contents[0].content : "", new { @class = "input", style = "width:100%;height:300px;", datatype = "*0-1000", sucmsg = " " })
                            <span class="Validform_checktip">*最多1000个字符</span>
                        </div>
                    </div>
                    <div class="sound layui-form-item" style="display:none;">
                        <label class="layui-form-label">语音标题</label>
                        <div class="layui-input-block">
                            @Html.TextBox("txtSoundTitle", Model != null && Model.response_type == 2 ? Model.contents[0].title : "", new { @class = "input normal", datatype = "*0-255", sucmsg = " " })
                            <span class="Validform_checktip">*最多30个字符</span>
                        </div>
                    </div>
                    <div class="sound layui-form-item" style="display:none;">
                        <label class="layui-form-label">文件地址</label>
                        <div class="layui-input-block">
                            @Html.TextBox("txtSoundUrl", Model != null && Model.response_type == 2 ? Model.contents[0].media_url : "", new { @class = "input normal upload-path" })
                            <div class="upload-box choose-pick"><a href="javascript:;" class="webuploader-pick" title="图片空间">选择图片</a></div>
                            <div class="upload-box upload-video"></div>
                            <span class="Validform_checktip">*MP3格式，填写链接或本地上传！</span>
                        </div>
                    </div>
                    <div class="sound layui-form-item" style="display:none;">
                        <label class="layui-form-label">语音描述</label>
                        <div class="layui-input-block">
                            @Html.TextArea("txtSoundContent", Model != null && Model.request_type == 2 ? Model.contents[0].content : "", new { @class = "input", datatype = "*0-500", sucmsg = " " })
                        </div>
                    </div>
                    <div class="picture layui-form-item" style="display:none;">
                        <label class="layui-form-label">图文列表</label>
                        <div class="layui-input-block"><a id="itemAddButton" class="layui-btn layui-btn-primary icon-btn"><i class="iconfont icon-pic"></i> 添加图片</a></div>
                    </div>
                    <div class="picture layui-form-item" style="display:none;">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-block">
                            <div class="table-container">
                                <table border="0" cellspacing="0" cellpadding="0" class="border-table" width="100%">
                                    <thead>
                                        <tr>
                                            <th width="12%">图片</th>
                                            <th width="20%">标题</th>
                                            <th width="40%">链接</th>
                                            <th width="12%">排序</th>
                                            <th width="10%">操作</th>
                                        </tr>
                                    </thead>
                                    <tbody id="item_box">
                                        @if (Model != null && Model.request_type == 2)
                                        {
                                            foreach (DTcms.Model.weixin_request_content item in Model.contents)
                                            {
                                                <tr class="td_c">
                                                    <td>
                                                        <input type="hidden" name="item_id" value="@item.id" />
                                                        <input type="hidden" name="item_content" value="@item.content" />
                                                        <input type="hidden" name="item_imgurl" value="@item.img_url" />
                                                        <span class="item_imgurl img-box">
                                                            @if (item.img_url.ToString() == "")
                                                            {
                                                                @:-
                                                            }
                                                            else
                                                            {
                                                                <img src="@item.img_url" />
                                                            }
                                                        </span>
                                                    </td>
                                                    <td>
                                                        <input type="hidden" name="item_title" value="@item.title" />
                                                        <span class="item_title">@item.title</span>
                                                    </td>
                                                    <td>
                                                        <input type="hidden" name="item_linkurl" value="@item.link_url" />
                                                        <span class="item_linkurl">@item.link_url</span>
                                                    </td>
                                                    <td>
                                                        <input type="hidden" name="item_sortid" value="@item.sort_id" />
                                                        <span class="item_sortid">@item.sort_id</span>
                                                    </td>
                                                    <td>
                                                        <a title="编辑" class="img-btn edit operator" onclick="showImgDialog(this);">编辑</a>
                                                        <a title="删除" class="img-btn del operator" onclick="delItemTr(this);">删除</a>
                                                    </td>
                                                </tr>
                                            }
                                        }
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--/内容-->
    <!--工具栏-->
    <div class="page-footer">
        <div class="btn-wrap">
            <button class="layui-btn icon-btn" id="btnSubmit"><i class="layui-icon layui-icon-ok"></i>提交保存</button>
            @if (Model != null)
            {
                <button class="layui-btn layui-btn-danger" id="btnDelete"><i class="layui-icon layui-icon-delete"></i>删除消息</button>
            }
            <input name="btnReturn" type="button" value="返回上一页" class="layui-btn layui-btn-warm" onclick="javascript: history.back(-1);" />
        </div>
    </div>
    <!--/工具栏-->
</form>
@section Scripts{
    <script type="text/javascript" src="/scripts/libs/js/uploader.js"></script>
    <script type="text/javascript">
        layui.use(['validform','jqueryform', 'element', 'form', 'cascadeSelect', 'fileChoose'], function () {
            var form = layui.form, cascadeSelect = layui.cascadeSelect, fileChoose = layui.fileChoose;
            cascadeSelect.init('ddlAccountId', false);
            $('.choose-pick').click(function () {
                var obj = $(this);
                fileChoose.open({
                    fileUrl: '',
                    listUrl: '/admin_ajax/files',
                    num: 1,
                    dialog: {
                        offset: '60px'
                    },
                    onChoose: function (res) {
                        if (res[0].type != "file") {
                            layer.msg('我只让你选择图片格式的文件', { icon: 2, time: 2000 }, function () { });
                            return;
                        }
                        obj.siblings(".upload-path").val(res[0].url);
                        obj.siblings(".layer-photos").show().find("img").attr("src", res[0].url);
                    }
                });
            });
            //初始化表单验证
            $("#form1").initValidform('#btnSubmit', 1);

            //初始化上传控件
            $(".upload-img").InitUploader({ sendurl: "/upload_ajax/action", swf: "/scripts/libs/module/uploader/uploader.swf" });
            $(".upload-video").InitUploader({ sendurl: "/upload_ajax/action", swf: "/scripts/libs/module/uploader/uploader.swf", type: 2, chunked: true });

            form.on('select(account)', function (data) {
                document.location = "subscribe_edit?action=@ViewBag.action&id=" + data.value;
            });
            $("#btnDelete").click(function () {
                cmswh.request("SubscribeDel??action=@ViewBag.action&id=@ViewBag.id&type=" + $("input[name=hideRequestType]").val(), "GET");
            })
            //点击类型
            form.on('radio(responseType)', function (data) {
                showItemBox(data.value);
            });

            //添加按钮(点击绑定)
            $("#itemAddButton").click(function () {
                showImgDialog();
            });

            //显示相关的容器
            showItemBox();
        });

        //显示相关的容器
        function showItemBox(num) {
            var objNum = arguments.length;
            var selectVal = $("input[name='rblResponseType']:checked").val();
            if (objNum == 1) {
                selectVal = num;
            }
            switch (selectVal) {
                case "1": //图文
                    $(".text").hide();
                    $(".sound").hide();
                    $(".picture").show();
                    break;
                case "2": //语音
                    $(".text").hide();
                    $(".sound").show();
                    $(".picture").hide();
                    break;
                default: //文字
                    $(".text").show();
                    $(".sound").hide();
                    $(".picture").hide();
                    break;
            }
        }

        //创建窗口
        function showImgDialog(obj) {
            var objNum = arguments.length;
            var d = layer.open({
                type: 2,
                area: ['500px', '440px'],
                title: "添加图片",
                maxmin: true,
                content: '../dialog/dialog_picture',
                btn: ["确定", '关闭'],
                btn1: function (index, layero) {
                    var iframeWin = window[layero.find('iframe')[0]['name']];
                    var trHtml = iframeWin.execPicHtml(obj);
                    if (trHtml.length > 0) {
                        $("#item_box").append(trHtml);
                        layer.close(index);
                    }
                },
                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']];
                    //检查是否修改状态
                    if (objNum == 1) {
                        iframeWin.submitValue(obj);
                    }
                },
                cancel: function (index, layero) { }
            });
        }

        //删除节点
        function delItemTr(obj) {
            $(obj).parent().parent().remove();
        }
    </script>
}